﻿/// <reference path="http://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" />
/// This class inherits from ASP.NET Ajax Sys.UI.Behavior (see last line in file for code that inherits)
/// and is a simple info box inspired by the original code from http://pietschsoft.com/post/2010/11/30/Bing-Maps-Ajax-7-Adding-InfoBox-Support-via-Extension-to-MicrosoftMaps.aspx
(function ($m) {
    /* $m == Microsoft.Maps namespace */
    //register namespace with ASP.NET Ajax
    Type.registerNamespace("Microsoft.Maps.Ext");


    Microsoft.Maps.Ext.InfoBox = function (map) {
        this._domelement = null;
        this._title = null;
        this._description = null;
        //call base class initialize
        Microsoft.Maps.Ext.InfoBox.initializeBase(this, [map]);
    };

    //the class 'prototype' that describes the properties and methods of the class
    //private methods are marked with _MethodName purely to help you distiguish them
    Microsoft.Maps.Ext.InfoBox.prototype = {
        initialize: function () {

        },
        //methods
        //#region
        Show: function (e) {
            /* Get location of Pushpin */
            var loc = this.get_element().
            tryLocationToPixel(
                e.target.getLocation(), Microsoft.Maps.PixelReference.page
            );

            //set parent on infobox equal to this pushpin
            this.set_parent(e.target);

            //set new title
            this._title = e.target.title; //get title from pushpin objects new title property (defined below)

            if (this._domelement === null) {
                /* Create <div> to show as InfoBox */
                this._domelement = document.createElement("div");
                //add info box class
                Sys.UI.DomElement.addCssClass(this._domelement, 'infobox');
                //apend to body tag
                document.body.appendChild(this._domelement);

            }
            //create or reset HTML content
            this._domelement.innerHTML = '<div class="container"><h4>' + this._title + '</h4><div id="ibdesc">' + this._description + "</div></div>";

            /* Show InfoBox and set position*/
            Sys.UI.DomElement.setVisible(this._domelement, true);
            Sys.UI.DomElement.setLocation(this._domelement, loc.x + 20, loc.y - 50);

            //raise event
            this._raiseEvent('shown');
        },
        Hide: function () {
            /* Hide InfoBox from view */
            if (this._domelement !== null) {
                Sys.UI.DomElement.setVisible(this._domelement, false);
                //raise event
                this._raiseEvent('hidden');
            }
        },
        //#endregion

        //properties
        //#region
        get_Title: function () {
            return this._title;
        },
        set_Title: function (v) {
            this._title = v;
        },
        get_Description: function () {
            return this._description;
        },
        set_Description: function (v) {
            this._description = v;
            //update html
            var desc = $get("ibdesc", this._domelement);
            if (desc) desc.innerHTML = v;
        },
        get_Map: function () {
            return this.get_element();
        },
        get_DomElement: function () {
            return this._domelement;
        },
        //#endregion

        //events
        //#region
        add_shown: function (handler) {
            this.get_events().addHandler('shown', handler);
        },
        remove_shown: function (handler) {
            this.get_events().removeHandler('shown', handler);
        },
        add_hidden: function (handler) {
            this.get_events().addHandler('hidden', handler);
        },
        remove_hidden: function (handler) {
            this.get_events().removeHandler('hidden', handler);
        },
        _raiseEvent: function (eventName, eventArgs) {
            var handler = this.get_events().getHandler(eventName);
            if (handler) {
                if (!eventArgs) {
                    eventArgs = Sys.EventArgs.Empty;
                }
                handler(this, eventArgs);
            }
        }
        //#endregion

    }

    Microsoft.Maps.Ext.InfoBox.registerClass('Microsoft.Maps.Ext.InfoBox', Sys.UI.Control);




    /* Microsoft.Maps.Pushpin extensions */
    //these extend the bing maps api to use our infobox control above
    (function (proto) {
        if (!proto.setInfoBox) {
            proto.setInfoBox = function (infoBox) {
                this.removeInfoBox();
                this.infobox = infoBox;

                /* attach event handlers */

                this.infoboxMouseOverHandler = $m.Events.addHandler(
                    this,
                    "mouseover",
                    function (e) {
                        infoBox.Show(e);
                    });
                this.infoboxMouseOutHandler = $m.Events.addHandler(
                    this,
                    "mouseout",
                    function (e) {
                        infoBox.Hide(e);
                    });
            };
        }
        if (!proto.removeInfoBox) {
            proto.removeInfoBox = function () {
                /* detach event handlers */
                $m.Events.removeHandler(this.infoboxMouseOverHandler);
                $m.Events.removeHandler(this.infoboxMouseOutHandler);
                this.infobox = null;
            };
        }
        if (!proto.title) {
            proto.title = null;
        }
    })($m.Pushpin.prototype);
})(Microsoft.Maps);